#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : xuexiaohua
# @time    : 2025/8/19 19:15
# @function: the script is used to do something.
# @version :
'''
完成企业微信接口特有的鉴权逻辑，获取access_token
'''
import os

import requests

from wework.apis.base_api import BaseApi
from wework.conftest import PROJECT_ROOT_DIR
from wework.utils.YamlUtils import YamlUtils


class WeWork(BaseApi):

    def __init__(self):
        # 方法一
        # self.base_url = "https://qyapi.weixin.qq.com"
        # 方法二
        self.get_config()
        self.token = self.get_wework_token()

    def get_wework_token(self):
        '''
        获取access_token
        :return:
        '''
        # # 企业id
        # corpid = "wwb4d7b0e6b06d93d2"
        # # 应用密钥
        # corpsecret = "iA2tqZpZaSvUidR09TnjjxtCPIylSa_V4ZVYS9MpoIQ"
        get_url = f"{self.base_url}/cgi-bin/gettoken"
        param = {
            "corpid": self.corpid,
            "corpsecret": self.corpsecret,
        }
        # 方法一
        r = requests.get(get_url, params=param)
        # 方法二：封装
        req = {
            "method": "GET",
            "url": get_url,
            "params": param
        }
        r = self.send_api(req)
        assert r.status_code == 200
        assert r.json()["errcode"] == 0
        return r.json()["access_token"]

    def get_config(self):
        '''
        读取配置文件数据
        :return:
        '''
        file_path = os.path.join(PROJECT_ROOT_DIR, "config", "test_env.yaml")
        yaml_data = YamlUtils.get_yaml_data(file_path)
        # 获取数据
        self.base_url = yaml_data["base_url"]
        self.corpid = yaml_data["corpid"]["tester"]
        self.corpsecret = yaml_data["corpsecret"]["contacts"]